package com.java.base.exercise.refelction;

import java.lang.reflect.Method;

/**
 * @Author RenPu
 * @Date 2023/10/3 16:28
 * @Version 1.0
 * @Description:
 **/
public class Test {


    private void privateMethod() {
        System.out.println("This is a private method.");
    }

    public static void main(String[] args) {
        try {
            Test test = new Test();
            Method[] methods = Test.class.getDeclaredMethods();

            for (Method method : methods) {
                if (method.getName().equals("privateMethod")) {
                    method.setAccessible(true); // 使私有方法可访问
                    method.invoke(test); // 调用私有方法
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
